package net.zjitc.action.User;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import net.zjitc.damain.User.User;
import net.zjitc.service.User.UserService;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;

/**
 * Created by Administrator on 2017/3/29.
 */
@Controller
@Scope("prototype")
public class UserAction extends ActionSupport implements ModelDriven<User> {

    @Resource
    private UserService userService;

    private User Model = new User();

    private String validatecode;//接收验证码

    public String getValidatecode() {
        return validatecode;
    }

    public void setValidatecode(String validatecode) {
        this.validatecode = validatecode;
    }

    /*注册界面*/
    public String registUI(){
        return "registUI";
    }



    public String regist(){
        String trueCode = (String) ActionContext.getContext().getSession().get("validateCode");

        if (trueCode.equalsIgnoreCase(validatecode)){
            userService.save(Model);
        }else {
            this.addFieldError("registcodefail","验证码不对");
            return "registfile";
        }
        return "regist";
    }
    /*对某一个方法进行校验*/
    //validateRegist 就是对regist进行校验
//    public void validateRegist() {
//        if (Model.getUsername()==null||"".equals(Model.getUsername())){
//            this.addFieldError("username","用户名不能为空");
//        }
//        if (Model.getPassword()==null||"".equals(Model.getPassword())){
//            this.addFieldError("password","密码不能为空");
//        }
//    }
    public String loginUI(){
        return "loginUI";
    }

    public String login(){
        User user = userService.findByUsernamePassword(Model.getUsername(),Model.getPassword());
        return "loginUI";
    }


    @Override
    public User getModel() {
        return Model;
    }

}
